//
//  SearchResultVC.m
//  Project
//
//  Created by 冯东辉 on 2018/9/18.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "SearchResultVC.h"
#import "MJRefresh.h"
#import "CollectCell.h"
#import "CollectHeadView.h"
#import "ModuleListVC.h"
#import "SsDetailVC.h"
#import "ModuleSsListVC.h"
#import "ModuleDetailVC.h"
#import "CityDetailVC.h"
#import "ZhiBiaoDetailVC.h"

@interface SearchResultVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *data;

@property (nonatomic, assign) NSInteger pageNo;


@end

@implementation SearchResultVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navTitleL.text = @"搜索结果";
    [self createBlueNavigationBar:YES];
    
    [self initSubViews];
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

#pragma mark - 没有数据
- (void)setNullDataView {
    
    UIImageView *nullDataV = [[UIImageView alloc] initWithFrame:CGRectMake((GET_SCREEN_WIDTH()-51)/2.0f, (GET_SCREEN_HEIGHT()-SafeAreaTopHeight-51)/2.0f, 51, 51)];
    nullDataV.image = [UIImage imageNamed:@"search_result_null"];
    [self.view addSubview:nullDataV];
    
    UILabel *nullDataL = [[UILabel alloc] initWithFrame:CGRectMake(0, nullDataV.bottom+30, GET_SCREEN_WIDTH(), 20)];
    nullDataL.text = @"未搜索到相关内容";
    nullDataL.textColor = UICOLOR_HEX(0xff666666);
    nullDataL.textAlignment = NSTextAlignmentCenter;
    nullDataL.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:nullDataL];
}

#pragma mark - init
- (void)initSubViews {
    
    [self.view addSubview:self.tableView];
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, SafeAreaTopHeight, GET_SCREEN_WIDTH()-20, GET_SCREEN_HEIGHT()-SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.showsVerticalScrollIndicator = NO;
        
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        // 设置文字
        [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
        [header setTitle:@"释放刷新" forState:MJRefreshStatePulling];
        [header setTitle:@"加载中..." forState:MJRefreshStateRefreshing];
        
        // 马上进入刷新状态
        [header beginRefreshing];
        
        // 设置刷新控件
        self.tableView.mj_header = header;
        
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        [footer setTitle:@"点击或拖动刷新" forState:MJRefreshStateIdle];
        [footer setTitle:@"加载更多..." forState:MJRefreshStateRefreshing];
        [footer setTitle:@"无更多数据" forState:MJRefreshStateNoMoreData];
        self.tableView.mj_footer = footer;
        [self.tableView.mj_footer setHidden:YES];
        
    }
    return _tableView;
}

- (void)loadNewData {
    
    _pageNo = 1;
    
    [[ProjectApi instance] getSearchInfoDataListWithCityName:_cityName keyWord:_keyWord page:[NSString stringWithFormat:@"%ld", _pageNo] callBack:^(NetModel *object) {
        
        [self.tableView.mj_header endRefreshing];
        
        if (object.isSuccessed) {
            
            
            [self.data removeAllObjects];
            self.data = nil;
            self.data = [NSMutableArray arrayWithArray:object.service];
            
            if (self.data && [self.data count] > 0) {
                
                [self.tableView.mj_footer setHidden:NO];
                [self.tableView.mj_footer resetNoMoreData];
                
            }else {
                
                self.tableView.mj_footer.hidden = YES;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                
                self.tableView.mj_header.hidden = YES;
                self.tableView.hidden = YES;
                [self setNullDataView];
            }
            
            [self.tableView reloadData];
            
        }else {
            
            [SVProgressHUD showErrorWithStatus:@"网络连接失败"];
            [SVProgressHUD dismissWithDelay:1.5f];
        }
    }];
}

- (void)loadMoreData {
    
    _pageNo = _pageNo+1;
    
    [[ProjectApi instance] getSearchInfoDataListWithCityName:_cityName keyWord:_keyWord page:[NSString stringWithFormat:@"%ld", _pageNo] callBack:^(NetModel *object) {
        
        [self.tableView.mj_footer endRefreshing];
        
        if (object.isSuccessed) {
            
            NSArray *array = [NSArray arrayWithArray:object.service];
            
            if (array && [array count] > 0) {
                
                [self.tableView.mj_footer endRefreshing];
                
                for (int i = 0; i < [array count]; i++) {
                    
                    [self.data addObject:array[i]];
                }
                
            }else {
                
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                
                [SVProgressHUD showInfoWithStatus:@"暂无数据"];
                [SVProgressHUD dismissWithDelay:1.5f];
            }
            
            [self.tableView reloadData];
            
        }else {
            
            self.pageNo = self.pageNo-1;
            [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
            [SVProgressHUD dismissWithDelay:1.5f];
        }
    }];
}

#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 1;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    static NSString *identifier_head = @"collect_head_view";
    
    CollectHeadView *headV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier_head];
    
    if (headV == nil) {
        
        headV = [[CollectHeadView alloc] initWithReuseIdentifier:identifier_head];
    }
    
    NSDictionary *dic = _data[section];
    
    NSString *dimName = [NSString stringWithFormat:@"%@", dic[@"dim_name"]];
    
    headV.iconV.image = [UIImage imageNamed:[NSString stringWithFormat:@"collect_%@", dimName]];
    headV.titleL.text = [NSString stringWithFormat:@"%@", dimName];
    
    headV.colorBaseBtn.tag = 999+section;
    [headV.colorBaseBtn addTarget:self action:@selector(sectionModuleClick:) forControlEvents:UIControlEventTouchUpInside];
    
    for (id layer in headV.colorBaseBtn.layer.sublayers) {
        
        if ([layer isKindOfClass:[CAGradientLayer class]]) {
            [layer removeFromSuperlayer];
            break;
        }
    }
    
    // 业务发展  无线接入  无线覆盖  无线语音业务  无线数据业务  无线网络结构  基站退服  宽带  魔盒
    if ([dimName isEqualToString:@"业务发展"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xfffa796f) endColor:UICOLOR_HEX(0xfff2a356) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"无线接入"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff2895f6) endColor:UICOLOR_HEX(0xff6341f9) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"无线覆盖"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff4e89fb) endColor:UICOLOR_HEX(0xff896af8) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"无限语音业务"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff2cb6ac) endColor:UICOLOR_HEX(0xff63e09d) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"无线数据业务"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff167ffb) endColor:UICOLOR_HEX(0xff20befc) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"无线网络结构"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xffd670b8) endColor:UICOLOR_HEX(0xffa663d4) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"基站退服"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xfff16a81) endColor:UICOLOR_HEX(0xffe270ab) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"宽带业务"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xfff4a24f) endColor:UICOLOR_HEX(0xfffc7e6a) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"传输网络"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xfffc7e6a) endColor:UICOLOR_HEX(0xfff4a24f) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"魔百盒业务"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff60e39a) endColor:UICOLOR_HEX(0xff25bdab) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else {
        
        // 实时数据
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff169bfa) endColor:UICOLOR_HEX(0xff9976e5) frame:headV.colorBaseBtn.bounds] atIndex:0];
    }
    
    return headV;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = _data[indexPath.section];
    NSDictionary *resultDic = [dic[@"list"] objectAtIndex:indexPath.row];
    
    NSString *dim_Name = dic[@"dim_name"];
    
    if ([dim_Name isEqualToString:@"业务发展"]) {
        
        ZhiBiaoDetailVC *vc = [[ZhiBiaoDetailVC alloc] init];
        vc.moduleName = resultDic[@"inD_Name"];
        vc.cityName = _cityName;
        vc.s_id = resultDic[@"s_id"];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if ([dim_Name isEqualToString:@"实时指标"]) {
        
        SsDetailVC *vc = [[SsDetailVC alloc] init];
        vc.moduleName = resultDic[@"inD_Name"];
        vc.cityName = _cityName;
        vc.s_id = resultDic[@"s_id"];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else {
        
        if ([_cityName isEqualToString:@"全省"]) {
            
            ModuleDetailVC *detailVC = [[ModuleDetailVC alloc] init];
            detailVC.moduleName = resultDic[@"inD_Name"];
            detailVC.s_id = resultDic[@"s_id"];
            detailVC.cityName = _cityName;
            [self.navigationController pushViewController:detailVC animated:YES];
            
        }else {
            
            CityDetailVC *vc = [[CityDetailVC alloc] init];
            vc.moduleName = resultDic[@"inD_Name"];
            vc.cityName = _cityName;
            vc.s_id = resultDic[@"s_id"];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

#pragma mark - tableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return [_data count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_data[section][@"list"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifierA = @"collect_list_cell";
    
    CollectCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierA];
    
    if (cell == nil) {
        
        cell = [[CollectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierA];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
    }
    
    [cell reloadSubViewsWithData:_data indexPath:indexPath];
    
    return cell;
}

#pragma mark - 进入维度列表页
- (void)sectionModuleClick:(UIButton *)btn {
    
    NSInteger tag = btn.tag - 999;
    
    NSDictionary *dic = _data[tag];
    
    NSString *moduleName = [NSString stringWithFormat:@"%@", dic[@"dim_name"]];
    
    if ([moduleName isEqualToString:@"实时指标"]) {
        
        ModuleSsListVC *ssListVc = [[ModuleSsListVC alloc] init];
        ssListVc.cityName = _cityName;
        ssListVc.type = @"1";
        ssListVc.h = @"24";
        [self.navigationController pushViewController:ssListVc animated:YES];
        
    }else {
        
        ModuleListVC *listVC = [[ModuleListVC alloc] init];
        listVC.moduleName = moduleName;
        listVC.sdim_ID = [NSString stringWithFormat:@"%@", dic[@"dimid"]];
        listVC.cityName = _cityName;
        listVC.type = @"1";
        [self.navigationController pushViewController:listVC animated:YES];
    }
}

/**
 导航栏返回事件
 */
-(void)leftNavigationBarButtonClick {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
